package com.everyline.lib_base.view

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.widget.ScrollView
import com.umeng.commonsdk.stateless.UMSLEnvelopeBuild.mContext


/**
 * Created by on 2021/2/18.
 * 天天挂线
 *  746608431@qq.com
 *  描述:TODO
 */

class ScrollViewHeight @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    ScrollView(context, attrs, defStyleAttr) {
    var heightMeasure: Int = 0

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        heightMeasure = heightMeasureSpec

        try {
            //最大高度显示为屏幕内容高度的一半
            val display = context.display
            val d = DisplayMetrics()
            display!!.getMetrics(d)
            //此处是关键，设置控件高度不能超过屏幕高度一半（d.heightPixels / 2）（在此替换成自己需要的高度）
            heightMeasure = MeasureSpec.makeMeasureSpec(d.heightPixels / 2, MeasureSpec.AT_MOST)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        //重新计算控件高、宽
        //重新计算控件高、宽
        super.onMeasure(widthMeasureSpec, heightMeasure)


    }
}